package com.bjb.vr.operation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjb.vr.common.ao.UpgradeAo;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.SysUpgradeService;
import com.bjb.vr.common.vo.ResGroupVo;
import com.bjb.vr.common.vo.SysTenantVo;
import com.bjb.vr.common.vo.UpgradeVo;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 系统升级模块
 *
 * @description:
 * @author: hujingbo
 * @create: 2022-07-22 16:52
 **/
@RestController
@RequestMapping("/api/operation/v1")
public class SysUpgradeController {

    @Resource
    SysUpgradeService upgradeService;


    /**
     * 升级客户列表查询
     *
     * @param upgradeAo
     * @return
     */
    @PostMapping("/upgrade/tenant/list")
    public BaseResult queryTenantList(@RequestBody UpgradeAo upgradeAo) {
        IPage<SysTenantVo> voIPage = upgradeService.queryTenantList(upgradeAo);
        return CommonResult.Ok().setResult(voIPage);
    }

    /**
     * 升级包列表查询
     *
     * @param upgradeAo
     * @return
     */
    @PostMapping("/upgrade/list")
    public BaseResult queryUpgradeList(@RequestBody UpgradeAo upgradeAo) {
        IPage<UpgradeVo> voIPage = upgradeService.queryUpgradeList(upgradeAo);
        return CommonResult.Ok().setResult(voIPage);
    }

    /**
     * 发布升级包数据
     *
     * @param upgradeAo
     * @return
     */
    @PostMapping("/upgrade/insert")
    public BaseResult insert(@RequestBody UpgradeAo upgradeAo) {
        // 校验数据
        BaseResult result = upgradeService.checkVersion(upgradeAo);
        if (!result.isSuccess()) {
            return CommonResult.Fail(result.getMessage());
        }
        // 发布数据
        boolean success = upgradeService.insert(upgradeAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }

    /**
     * 编辑升级包数据
     *
     * @param upgradeAo
     * @return
     */
    @PostMapping("/upgrade/update")
    public BaseResult update(@RequestBody UpgradeAo upgradeAo) {
        Assert.notNull(upgradeAo.getId(), "主键ID不能为空");
        boolean success = upgradeService.updateInfo(upgradeAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 查询单个升级包数据
     *
     * @param upgradeAo
     * @return
     */
    @PostMapping("/upgrade/queryOne")
    public BaseResult queryOne(@RequestBody UpgradeAo upgradeAo) {
        Assert.notNull(upgradeAo.getId(), "主键ID不能为空");
        UpgradeVo upgradeVo = upgradeService.queryOne(upgradeAo);
        return CommonResult.Ok().setResult(upgradeVo);
    }

    /**
     * 删除单个升级包数据
     *
     * @param upgradeAo
     * @return
     */
    @PostMapping("/upgrade/delete")
    public BaseResult delete(@RequestBody UpgradeAo upgradeAo) {
        Assert.notNull(upgradeAo.getId(), "主键ID不能为空");
        boolean success = upgradeService.delete(upgradeAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_REMOVE);
        }
        return CommonResult.Ok();
    }

    /**
     * 触发升级
     *
     * @param upgradeAo
     * @return
     */
    @PostMapping("/upgrade/release")
    public BaseResult release(@RequestBody UpgradeAo upgradeAo) {
        Assert.notNull(upgradeAo.getId(), "主键ID不能为空");
        Assert.notNull(upgradeAo.getPublishStatus(), "发布状态不能为空");
        boolean success = upgradeService.release(upgradeAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }
}
